package com.example.app1;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.w3c.dom.Text;

public class SelectButtonActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private CheckBox default_ckb;
    private CheckBox custom_ckb;
    private TextView tv_default_switch;
    private TextView tv_custom_switch;
    private TextView tv_sex;
    private RadioGroup rg;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selectbutton);
        default_ckb = findViewById(R.id.default_ckb);
        default_ckb.setOnCheckedChangeListener(this);
        custom_ckb = findViewById(R.id.custom_ckb);
        custom_ckb.setOnCheckedChangeListener( this);
        CompoundButton default_switch = findViewById(R.id.default_switch);
        CompoundButton custom_switch=findViewById(R.id.custom_switch);
        default_switch.setOnCheckedChangeListener(this);
        custom_switch.setOnCheckedChangeListener(this);
        tv_default_switch = findViewById(R.id.tv_default_switch);
        tv_custom_switch = findViewById(R.id.tv_custom_switch);
        tv_sex = findViewById(R.id.tv_sex);
        rg = findViewById(R.id.rg_sex);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.rb_man)
                    tv_sex.setText("宁是个帅气的大男孩");
                else if (checkedId==R.id.rb_women) {
                    tv_sex.setText("宁是一个甜甜小妹");
                }
            }
        });

    }


    @Override
    public void onCheckedChanged(CompoundButton button, boolean isChecked) {
        switch (button.getId())
        {
            case R.id.default_ckb:
            case R.id.custom_ckb:
                String str = String.format("您%s了这个复选框", isChecked ? "勾选了" : "取消勾选");
                button.setText(str);
                break;
            case R.id.default_switch:
                String str1=String.format("默认开关（状态：%s）：",isChecked?"开":"关");
                tv_default_switch.setText(str1);
                break;
            case R.id.custom_switch:
                String str2=String.format("自定义开关（状态：%s）：",isChecked?"开":"关");
                tv_custom_switch.setText(str2);
                break;
        }
    }
}
